<template>
  <div class="app-container">
    <a-card class="general-card" title="打卡信息">
      <a-row>
        <a-col :flex="1">
          <a-form
              :model="queryParams"
              ref="queryRef"
              :inline="true"
              v-show="showSearch"
              :label-col-props="{ span: 6 }"
              :wrapper-col-props="{ span: 18 }"
              label-align="left"
          >
            <a-row :gutter="16">
              <a-col :span="8">
                <a-form-item label="创建时间" field="createTime">
                  <a-range-picker
                      v-model="dateRange"
                      style="width: 100%"
                  />
                </a-form-item>
              </a-col>
            </a-row>
          </a-form>
        </a-col>
        <a-divider style="height: 84px" direction="vertical" />
        <a-col :flex="'86px'" style="text-align: right">
          <a-space direction="vertical" :size="18">
            <a-button type="primary" @click="handleQuery">
              <template #icon>
                <icon-search />
              </template>
              搜索
            </a-button>
            <a-button @click="resetQuery">
              <template #icon>
                <icon-refresh />
              </template>
              重置
            </a-button>
          </a-space>
        </a-col>
      </a-row>
    </a-card>

    <a-table
        v-model:selectedKeys="selectedKeys"
        :row-selection="rowSelection"
        row-key="id"
        :loading="loading"
        :pagination="false"
        :data="customerInfoList"
        :bordered="false"
        @selection-change="handleSelectionChange"
    >
      <template #columns>
        <a-table-column
            fixed="left"
            title="用户名" align="center" data-index="userName" :width="175" />
        <a-table-column data-index="signType" title="打卡类型" width="175">
          <template #cell="{record}">
            <dict-tag :options="bis_sign_type" :value="record.signType" />
          </template>
        </a-table-column>
        <a-table-column title="打卡日期" align="center"  data-index="clockDate" :width="175" />
        <a-table-column
            title="打卡时间" align="center" data-index="createTime" :width="175" />
        <a-table-column title="操作" align="center" class-name="small-padding fixed-width">
          <template #cell="{record}">
            <a-button
                type="text"
                @click="handleView(record)"
            >
              <icon-edit/>
              查看
            </a-button>
          </template>
        </a-table-column>
      </template>
    </a-table>

    <!--分页-->
    <div style="margin-top: 30px;float: right">
      <a-pagination :total="total" show-total show-jumper show-page-size
                    v-model:current="queryParams.pageNum"
                    v-model:page-size="queryParams.pageSize"
                    @change="onPageChange"
                    @page-size-change="onPageSizeChange"
      />
    </div>

    <!-- 查看家庭信息-->
    <a-drawer :visible="viewDrawerVisible" @ok="viewDrawerVisible=false" @cancel="viewDrawerVisible=false"  ok-text="关闭" hide-cancel  unmountOnClose :width="540">
      <template #title>
        <h4>查看打卡信息</h4>
      </template>
      <a-form ref="dictRef" :model="form" :rules="rules" label-width="80px">
        <a-form-item label="用户名" field="userName">
          <span>{{ form.userName }}</span>
        </a-form-item>
        <a-form-item label="打卡类型" field="signType">
           <span>
              <dict-tag :options="bis_sign_type" :value="form.signType" />
            </span>
        </a-form-item>
        <a-form-item label="打卡日期" field="clockDate">
          <span>{{ form.clockDate }}</span>
        </a-form-item>
        <a-form-item label="打卡时间" field="createTime">
          <span>{{ form.createTime }}</span>
        </a-form-item>
      </a-form>
      <div style="height: 350px"><MapLocation :lon="form.lon" :lat="form.lat"/></div>
    </a-drawer>


  </div>
</template>

<script setup name="CustomerList">
import {getCurrentInstance, reactive, ref} from "vue";
import MapLocation from '../housekeeper/map-location'
import {getSignInfoList,getSign} from "@/api/grid/sign";

const selectedKeys = ref([]);
const rowSelection = reactive({
  type: 'checkbox',
  showCheckedAll: true,
});
const { proxy } = getCurrentInstance();
const { bis_sign_type } = proxy.useDict("bis_sign_type");

const customerInfoList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const dateRange = ref([]);
const viewDrawerVisible = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
});

const { queryParams, form,rules } = toRefs(data);

const onPageChange = (current) => {
  queryParams.value.pageNum = current
  getList()
};
const onPageSizeChange = (pageSize) => {
  queryParams.value.pageSize = pageSize
  getList()
};

/** 查看按钮操作 */
function handleView(row) {
  reset();
  const id = row.id || ids.value;
  getSign(id).then(response => {
    form.value = response.data;
    viewDrawerVisible.value = true;
  });
}

/** 查询新设备申请列表 */
function getList() {
  queryParams.value.startTime = dateRange.value[0];
  queryParams.value.endTime = dateRange.value[1];
  loading.value = true;
  getSignInfoList(queryParams.value).then(response => {
    customerInfoList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  queryParams.value = {}
  form.value = {
    name: undefined,
    phone: undefined,
    remark: undefined
  };
  proxy.resetForm("equipmentApplicationRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
getList();
</script>
